package com.situ.mall.controller;
import com.situ.mall.common.util.AliOSSUtil;
import com.situ.mall.common.util.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

//图片上传
@RestController
@RequestMapping("/admin")//让网关的路由知道你是哪个微服务下面的
public class UploadController {
    @RequestMapping("/upload")
    public Result upload(MultipartFile file) {//file是图片转化的io流
        //上传到阿里云的图片用uuid作为名字
        //cb32c37e8c2744559c7fe527277be072
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //原名称：a.png
        String filename = file.getOriginalFilename();
        //加上后缀.png,变量名extension扩展名，额外的
        String extension = filename.substring(filename.lastIndexOf("."));
        //cb32c37e8c2744559c7fe527277be072.png得到完整名字newFileName
        String newFileName = uuid + extension;
        //url为图片在阿里云的地址
        String url = "";
        //用AliOSSUtil.uploadFile把（名字，io流）上传到阿里云
        try {
            url = AliOSSUtil.uploadFile(newFileName,file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.ok("",url);//将url地址返回给前台获取图片
    }

    public static void main(String[] args) {
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        System.out.println(uuid.replace("-",""));
    }

}
